#define _CRT_SECURE_NO_WARNINGS

#include"game.h"

void INITarr(char arr[ROW][COL], int row, int col)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < row; i++)
 {
  for (j = 0; j < col; j++)
  {
   arr[i][j] = ' ';
  }
 }
}
void Displayarr(char arr[ROW][COL], int row, int col)
{
 int i = 0;
 for (i = 0; i < row; i++)
 {
  //打印数据
  int j = 0;
  for (j = 0; j < col; j++)
  {
   printf(" %c ", arr[i][j]);
   if (j < col - 1)
   {
    printf("|");
   }
  }
  printf("\n");


  //打印分割线
  if (i < row - 1)
  {
   int j = 0;
   for (j = 0; j < col; j++)
   {
    printf("---");
    if (j < col - 1)
     printf("|");
   }
   printf("\n");
  }
 }
}

void playmove(char arr[ROW][COL], int row, int col)
{
 int x = 0;
 int y = 0;
 printf("玩家走\n");
 while (1)
 {
  
  printf("请输入坐标");
  scanf("%d %d", &x, &y);
  if (x >= 1 && x <= row && y >= 1 && y <= col)
  {
   if (arr[x - 1][y - 1] == ' ')
   {
    arr[x - 1][y - 1] = '*';
    break;
   }
   else
    printf("已被占用");

  }
  else
   printf("非法输入");
 }

}

void compertermove(char arr[ROW][COL],int row,int col)
{
 int x = rand() % row;
 int y = rand() % col;
 printf("电脑走\n");
 while (1)
 {
  if (arr[x][y] == ' ')
  {
   arr[x][y] = '#';
   break;
  }
 }
}

int Isfull(char arr[ROW][COL], int row, int col)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < row; i++)
 {
  for (j = 0; j < col; j++)
  {
   if (arr[i][j] == ' ')
    return 0;
  }
 }
 return 1;
}

char Orwin(char arr[ROW][COL], int row, int col)
{
 
  int i = 0;
  int j = 0;
  int count = 0;
  //行是否相等
  for (i = 0; i < row; i++) {
   count = 0;
   for (j = 0; j < col - 1; j++) {
    if (arr[i][j] == arr[i][j + 1] && arr[i][j] != ' ') {
     count++;
    }
    else
    {
     break;
    }
    //if (count == col - 1) {
    // return board[i][0];
    //}
   }
   if (count == col - 1) {
    return arr[i][0];
   }
  }

  //列
  //for (i = 0; i < col; i++) {
  // if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
  //  return board[0][i];
  // }
  //}
  for (i = 0; i < col; i++) {
   count = 0;
   for (j = 0; j < row - 1; j++) {
    if (arr[j][i] == arr[j + 1][i] && arr[j][i] != ' ') {
     count++;
    }
    else
    {
     break;
    }
    //if (count == row - 1) {
    // return board[0][i];
    //}
   }
   if (count == row - 1) {
    return arr[0][i];
   }
  }

  //对角线
  //if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') {
  // return board[0][0];
  //}
  //if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') {
  // return board[1][1];
  //}

  //分别判断两条对角线
  int count1 = 0;
  for (i = 0; i < row - 1; i++) {
   //count = 0;
   if ((arr[i][i] == arr[i + 1][i + 1]) && (arr[0][0] != ' ')) {//循环判断第一条对角线 如果相同计数器累加 不相同就跳出循环
    count1++;
   }
   else
   {
    break;
   }
   //if (count1 == row - 1) {//到此时 第一条对角线上所有元素相同 游戏结束 返回胜利条件 count达不到row-1！！
   // return board[0][0];
   //}
  }
  if (count1 == row - 1) {//到此时 需要在外层判断！！ 第一条对角线上所有元素相同 游戏结束 返回胜利条件
   return arr[0][0];
  }
  int count2 = 0;
  for (i = 0; i < row - 1; i++) {
   //count = 0;
   if (arr[i][row - 1 - i] == arr[i + 1][row - 2 - i] && arr[row - 1 - i][0] != ' ') {
    count2++;
   }
   else
   {
    break;
   }
   //if (count2 == row - 1) {
   // return board[row-1][0];
   //}
  }
  if (count2 == row - 1) {
   return arr[row - 1][0];
  }
  //判断平局 棋盘是否满了 是否有空格
  if (Isfull(arr, ROW, COL) == 1) {
   return 'Q';
  }
  //不是平局
  return 'C';
 }